// 重置密码
function resetPassword() {
    pass1 = document.getElementById("pass1").value
    pass2 = document.getElementById("pass2").value

    if (pass1 != pass2) {
        error_bar("两次密码不一致啊")
        return
    }
    if (pass1.length < 1 || pass2.length > 20) {
        error_bar("密码应该 1-20 位")
        return
    }
    
    $.post("/user/api/set/password", {
        password: pass1
    },function(data) {
        success_bar(data.msg)
    })
}


function getP() {
    p = new Object()
    for (i=0; i<items.length; i++) {
        p[items[i].e] = document.getElementById(items[i].e).value
    }
    p.sex = document.getElementById('sex').value
    p.gov = document.getElementById('gov').value
    p.has = document.getElementById('has').value
    return p
}
// 保存信息gov-select
function saveInfo() {
    p = getP()
    $.post("/user/api/set/info",p,function(data) {
         success_bar(data.msg)
    })
}

getInfo()
function getInfo() {
    $.get("/user/api/set/info",function(data) {
        o = new Object()
        if (data == "") {
            for (i=0; i<items.length; i++) {
                o[items[i].e] = ""
            }
            o.sex = 2
            o.gov = 0
            o.has = 0
            data = o
        } else {
            data = JSON.parse(data)
        }
        for (let ele in data) {
            switch (ele) {
                case "sex":
                    text = `
                    <label class="mdui-textfield-label">性别</label>
                    <select class="mdui-select" mdui-select id="sex">
                        <option value="2">请选择性别</option>
                        <option value="0">女</option>
                        <option value="1">男</option>
                    </select>`
                    text = text.replace(data[ele]+`">`,data[ele]+`" selected>`)
                    document.getElementById("choose_div").innerHTML += text
                    break
                case "gov":
                    text = ` <label class="mdui-textfield-label">政治面貌</label>
                    <select class="mdui-select" mdui-select id="gov">
                        <option value="0">请选择政治面貌</option>
                        <option value="1">尊贵的群众</option>
                        <option value="2">伟大的团员</option>
                        <option value="3">最强的党员</option>
                    </select>`
                    text = text.replace(data[ele]+`">`,data[ele]+`" selected>`)
                    document.getElementById("choose_div").innerHTML += text
                    break
                case "has":
                    text = `
                    <label class="mdui-textfield-label">是否单身</label>
                    <select class="mdui-select" mdui-select id="has">
                        <option value="0">单身贵族</option>
                        <option value="1">低贱的恋爱狗</option>
                    </select>`
                    text = text.replace(data[ele]+`">`,data[ele]+`" selected>`)
                    document.getElementById("choose_div").innerHTML += text
                    break
            }
            document.getElementById(ele).value = data[ele]
        }
        
        mdui.mutation()
    })
}

function initFileChoser() {
    $('#upload').fileupload({
        url: '/user/api/set/facer',
        //dataType: 'json'
    
        // 上传完成后的执行逻辑
        done: function (e, data) {
            window.location.reload()
        },
    
        // 上传过程中的回调函数
        progressall: function (e, data) {
            
        },
    
        // 开始上传
        start: function (e, data) {
            
        }
    });
}

function exitLogin() {
    window.localStorage.clear()
    $.get("/user/api/exit",function(){window.location.reload()})
}